package com.lagou.hdfs.hbase.processor;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;

public class UserDeleteProcessor extends BaseRegionObserver {
    @Override
    public void postDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit, Durability durability) throws IOException {
        //获取t2表table对象
        final HTable t2 = (HTable) e.getEnvironment().getTable(TableName.valueOf("userfollower"));
        //解析t1表的插入对象put
        List<Delete> deletes = new ArrayList<>();
        NavigableMap<byte[], List<Cell>> familyCellMap = delete.getFamilyCellMap();
        //执行删除
        familyCellMap.forEach((key, value) -> {
            value.forEach(item -> {
                Delete delete1 = new Delete(item.getQualifier());
                delete.addColumn(key, delete.getRow());
                deletes.add(delete1);
            });
        });
        for (Delete delete1 : deletes) {
            t2.delete(delete1);
        }
        //关闭table对象
        t2.close();
        System.out.println("删除数据成功！！");
    }
}
